﻿using Base.Infrastructure.Dto.Request.option;
using Base.Infrastructure.Dto.Request.Topic;
using Base.Infrastructure.Dto.Response;
using Base.Infrastructure.EFCore.MySql;
using Base.Infrastructure.IOC;
using Base.Infrastructure.Tools;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base.Service.Instance
{

    public class QuestionService : BaseService, Base.Service.Interface.IQuestionService
    {
        private readonly QuestionIOC _questionIOC;
        private optionIOC _optionIOC;

        public QuestionService(QuestionIOC questionIOC, optionIOC optionIOC, IConfiguration configuration)
        {
            _questionIOC = questionIOC;
            _optionIOC = optionIOC;
        }

        /// <summary>
        /// 添加题目
        /// </summary>
        /// <param name="question"></param>
        /// <returns></returns>
        private bool AddQuestion(Question question)
        {
            _questionIOC._questionService.Add(question);
            return _questionIOC._questionService.SaveChanges() > 0;
        }

        /// <summary>
        /// 添加选项
        /// </summary>
        /// <param name="question"></param>
        /// <returns></returns>
        private bool AddOption(Option option)
        {
            _optionIOC._optionService.Add(option);
            return _optionIOC._optionService.SaveChanges() > 0;
        }

        /// <summary>
        /// 添加题目和选项
        /// </summary>
        /// <param name="question"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult AddQuestion(QuestionRequestDto question)
        {
            string qid = Config.GUID();
            //添加题目
            this.AddQuestion(new Question
            {
                Qid = qid,
                Qcontent = question.Qcontent,
                Qexplain = question.Qexplain,
                Qsubject = question.Qsubject,
                Qtype = question.Qtype,
                Qlv = question.Qlv,
                QisPublish = question.QisPublish,
                QisBan = question.QisBan,
                QcreateTime = DateTime.Now.ToString()
            });
            //判断题目类型添加选项
           
                var Ocontent = "";
                var OisRight = 0;
                for (int i = 0; i < 4; i++)
                {
                    if (i == 0)
                    {
                        if (question.OisRight == "A")
                        {
                            OisRight = 1;
                        }
                        Ocontent = question.OcontentA;
                    }
                    else if (i == 1)
                    {
                        if (question.OisRight == "B")
                        {
                            OisRight = 1;
                        }
                        Ocontent = question.OcontentB;
                    }
                    else if (i == 2)
                    {
                        if (question.OisRight == "C")
                        {
                            OisRight = 1;
                        }
                        Ocontent = question.OcontentC;
                    }
                    else
                    {
                        if (question.OisRight == "D")
                        {
                            OisRight = 1;
                        }
                        Ocontent = question.OcontentD;
                    }
                    //添加选项
                    string oid = Config.GUID();
                    this.AddOption(new Option
                    {
                        Oid = oid,
                        Oqid = qid,
                        Ocontent = Ocontent,
                        OisRight = OisRight
                    });
                }
                return GetResult(1, "添加成功");
      
           
        }
    }
}
